package queue;


import java.util.LinkedList;

public class MyQueue {
    static class Node{
        private int val;
        private Node next;

        public Node(int val) {
            this.val = val;
        }
    }
    public Node head;
    public Node last;
    public int usedSize;
    public void offer(int data){ // 从队尾插入元素
        Node node = new Node(data);
        if(head == null){
            head = node;
            last = node;
        } else {
            last.next = node;
            last = node;
        }
        usedSize++;
    }
    public int poll(){  // 从队头删除元素
        if(empty()){
            throw new QueueNullException("队列为空");
        }
        int data = head.val;
        head = head.next;
        if(head == null){
            last = null;
        }
        usedSize--;
        return data;
    }
    public int peek(){  // 获取队头元素
        if(empty()){
            throw new QueueNullException("队列为空");
        }
        return head.val;
    }
    public boolean empty(){ // 判断是否为空
        return usedSize == 0;
    }
}
